//
//  FeedbackAddViewController.m
//  jungoo
//
//  Created by Dang Quang Trung on 8/8/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "FeedbackAddViewController.h"
#import "User.h"
#import "AppDelegate.h"
#import "Request.h"

@implementation FeedbackAddViewController

@synthesize txtContent,type;

-(void)applyAppearance 
{
    [self setBackButtonImageActiveName:nil 
                          inactiveName:@"back-button" 
                                target:self 
                                action:@selector(backPressed)];
    [self setRightButtonImageActiveName:nil 
                           inactiveName:@"done-button" 
                                 target:self 
                                 action:@selector(editPressed)];    
    
    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"home_bg.png"]]]; 
}

-(void) backPressed 
{    
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)editPressed
{    
    if ([txtContent.text length]) {
        [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
        [JGoUtility showProgressInView:[JGoUtility frontFromView:self.view]
                                target:self 
                                 title:@"Sending feeback..." 
                              selector:@selector(delayFeedback:)];
    }
    else {
        [JGoUtility showAlert:@"Please enter your feedback content"];
    }
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];    
}

- (NSString*)titleFromType
{
    switch (self.type) {
        case 0: {
            return @"Errors/Bugs";           
            break;
        }
        case 1: {
            return @"Ideas";
            break;                
        }
            
        case 2: {
            return @"Business Matters";
            break;
        }
        case 3: {
            return @"Questions";
            break;
        }
    }    
    
    return nil;
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{    
    txtContent.backgroundColor = [UIColor clearColor];
    txtContent.textColor = [UIColor blackColor];
    [self applyAppearance];    
    [self setTitle:[self titleFromType]];
    [super viewDidLoad];
    [txtContent resignFirstResponder];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return NO;
}
-(void)dealloc
{
    [txtContent release];
    [super dealloc];
}

#pragma mark - Delay Method

- (void)delayFeedback:(MBProgressHUD*)progress
{
    NSString *userId = [[JGoUtility sharedCreateAccount] objectForKey:@"user_id"];
    NSError *error = nil;
    
    [JGoAPI sendFeedbackWithUserId:userId 
                              type:[NSString stringWithFormat:@"%d",self.type] 
                           content:txtContent.text error:&error];
    
    if (error) {
        [JGoUtility showAlert:[error localizedDescription]];
    }
    
    [progress hide:YES];
}

@end
